from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser,CommaSeparatedListOutputParser
from langchain_openai import ChatOpenAI

#模型
mode = ChatOpenAI(model_name="qwen-plus",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key="sk-4f1498f1c0314ba79ea2919bd7a02c4d",
    temperature=0.7,
    streaming=True
)

#定义提示词
prompt = ChatPromptTemplate.from_template("写五首关于{topic}的诗词")

#创建解析器
# parser = StrOutputParser()
parser = CommaSeparatedListOutputParser()
chain = prompt | mode | parser

#调用
result = chain.invoke({"topic":"秋天"})
print(result)